package com.lagou.homework.model3;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import javax.crypto.NullCipher;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Hanpeng
 * @date 2021/12/23 19:58
 * @description: 小文件过多可以用合并小文件的inputformat
 */
public class NumSortDriver {
    public static void main(String[] args) throws Exception {
        // 1 获取配置信息以及封装任务
        Configuration configuration = new Configuration();
        Job job = Job.getInstance(configuration,"NumSortDriver");
        // 2 设置jar加载路径 比如WordcountDriver.class就包含在所打的jar包中
        job.setJarByClass(NumSortDriver.class);
        // 3 设置map和reduce类
        job.setMapperClass(NumSortMapper.class);
        job.setReducerClass(NumSortReducer.class);
        // 4 设置map输出
        job.setMapOutputKeyClass(IntWritable.class);
        job.setMapOutputValueClass(NullWritable.class);
        // 5 设置最终输出kv类型 也就是输出结果文件的类型
        job.setOutputKeyClass(IntWritable.class);
        job.setOutputValueClass(IntWritable.class);
        // 6 设置输⼊和输出路径
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]+"_"+
                new SimpleDateFormat("yyyyMMdd HHmmss").format(new Date())));
        // 7 提交
        boolean result = job.waitForCompletion(true);
        System.exit(result ? 0 : 1);
    }
}
